const router = require('koa-router')()
const userService = require('../mysql/mysqlConfig');
// router.prefix('/api')
router.get('/', async (ctx, next) => {
  ctx.body = 'ddddd'
  await ctx.render('index', {
    title: 'Hello 梁少聪!'
  })
})
router.post('/send', async (ctx, next) => {
  // ctx.request.body
  // let _sql = "'INSERT INTO todolist(id,completed,title) VALUES(?,?,?)"

  let arr = [];
  arr.push(ctx.request.body['id']);
  arr.push(ctx.request.body['title']);
  arr.push(ctx.request.body['completed']);

  await userService.addData(arr)
    .then(res => {
      if (res.affectedRows != 0) {
        ctx.body = {
          code: 200,
          msg: "添加成功",
        };
      }

    }).catch(() => {
      ctx.body = {
        code: 201,
        msg: "添加失败",
      }
    })

})

router.get('/gettolist', async (ctx, next) => {
  const res = await userService.findAllUser();
  ctx.body = {
    code: 200,
    result: res,
    msg: "查询成功",
  };
})

module.exports = router